//
//  UIView+Category.m
//  DCProject
//
//  Created by bigbing on 2019/4/1.
//  Copyright © 2019 bigbing. All rights reserved.
//

#import "UIView+Category.h"

#pragma mark - 分类 关于view.frame
@implementation UIView (Category)

- (CGFloat)dc_x {
    return self.frame.origin.x;
}


- (void)setDc_x:(CGFloat)dc_x {
    CGRect dcFrame = self.frame;
    dcFrame.origin.x = dc_x;
    self.frame = dcFrame;
}


- (CGFloat)dc_y {
    return self.frame.origin.y;
}


- (void)setDc_y:(CGFloat)dc_y {
    CGRect dcFrame = self.frame;
    dcFrame.origin.y = dc_y;
    self.frame = dcFrame;
}


- (CGPoint)dc_origin {
    return self.frame.origin;
}


- (void)setDc_origin:(CGPoint)dc_origin {
    CGRect dcFrame = self.frame;
    dcFrame.origin = dc_origin;
    self.frame = dcFrame;
}


- (CGFloat)dc_width {
    return self.frame.size.width;
}


- (void)setDc_width:(CGFloat)dc_width {
    CGRect dcFrame = self.frame;
    dcFrame.size.width = dc_width;
    self.frame = dcFrame;
}


- (CGFloat)dc_height {
    return self.frame.size.height;
}


- (void)setDc_height:(CGFloat)dc_height {
    CGRect dcFrame = self.frame;
    dcFrame.size.height = dc_height;
    self.frame = dcFrame;
}


- (CGSize)dc_size {
    return self.frame.size;
}


- (void)setDc_size:(CGSize)dc_size {
    CGRect dcFrame = self.frame;
    dcFrame.size = dc_size;
    self.frame = dcFrame;
}


- (CGFloat)dc_centerX {
    return self.center.x;
}


- (void)setDc_centerX:(CGFloat)dc_centerX {
    CGPoint dcFrmae = self.center;
    dcFrmae.x = dc_centerX;
    self.center = dcFrmae;
}

- (CGFloat)dc_centerY {
    return self.center.y;
}


- (void)setDc_centerY:(CGFloat)dc_centerY {
    CGPoint dcFrame = self.center;
    dcFrame.y = dc_centerY;
    self.center = dcFrame;
}


- (CGFloat)dc_right {
    return CGRectGetMaxX(self.frame);
}


- (CGFloat)dc_bottom {
    
    return CGRectGetMaxY(self.frame);
}


- (void)setDc_right:(CGFloat)dc_right {
    self.dc_x = dc_right - self.dc_width;
}


- (void)setDc_bottom:(CGFloat)dc_bottom {
    self.dc_y = dc_bottom - self.dc_height;
}


- (BOOL)intersectWithView:(UIView *)view {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect selfRect = [self convertRect:self.bounds toView:window];
    CGRect viewRect = [view convertRect:view.bounds toView:window];
    return CGRectIntersectsRect(selfRect, viewRect);
}


- (BOOL)isShowingOnKeyWindow {
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}


+ (instancetype)dc_viewFromXib {
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

#pragma mark - XIB设置圆角
- (void)setCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}

- (CGFloat)cornerRadius{
    return self.layer.cornerRadius;
}

#pragma mark - XIB 设置边框颜色和宽度
-(void)setBorderColor:(UIColor *)borderColor{
    self.layer.borderColor = borderColor.CGColor;
}
-(UIColor *)borderColor{
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

-(void)setBorderWidth:(CGFloat)borderWidth{
    self.layer.borderWidth = borderWidth;
}
-(CGFloat)borderWidth{
    return self.layer.borderWidth;
}

/**
 * @brief 移除此view上的所有子视图
 */
- (void)removeAllSubviews {
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    return;
}


-(void)setPriorityRequired{
    //抗拉伸性 越高越不容易被拉伸
    [self setContentHuggingPriority:UILayoutPriorityRequired
                                         forAxis:UILayoutConstraintAxisHorizontal];
    //抗压缩性 越高越不容易被压缩
    [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}


//#pragma mark - 简化设置对象内容
//
//+(UIView *)dc_init{
//    UIView * view = UIView.alloc.init;
//    return view;
//}
//-(DHViewIntegerBlock)dc_tag{
//    return ^(NSInteger tag){
//        self.tag = tag;
//        return self;
//    };
//}
//
//- (DHViewCGRectBlock)dc_frame{
//    return ^(CGRect rect){
//        self.frame = rect;
//        return self;
//    };
//}
//- (DHViewCGRectBlock)dc_bounds{
//    return ^(CGRect rect){
//        self.bounds = rect;
//        return self;
//    };
//}
//-(DHViewCGPointBlock)dc_center{
//    return ^(CGPoint point){
//        self.center = point;
//        return self;
//    };
//}
//-(DHViewViewBlock)dc_addSubview{
//    return ^(UIView * view){
//        [self addSubview:view];
//        return self;
//    };
//}
//- (DHViewColorBlock)dc_backgroundColor{
//    return ^(UIColor * color){
//        self.backgroundColor = color;
//        return self;
//    };
//}
//-(DHViewColorHexBlock)dc_backgroundColorHex{
//    return ^(NSString * colorHex){
//        
//        NSString *cString = [[colorHex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//        
//        // String should be 6 or 8 characters
//        if ([cString length] > 6) {
//            // 判断前缀
//            if ([cString hasPrefix:@"0X"])
//                cString = [cString substringFromIndex:2];
//            if ([cString hasPrefix:@"#"])
//                cString = [cString substringFromIndex:1];
//            if ([cString length] != 6)
//                return self;
//            // 从六位数值中找到RGB对应的位数并转换
//            NSRange range;
//            range.location = 0;
//            range.length = 2;
//            //R、G、B
//            NSString *rString = [cString substringWithRange:range];
//            range.location = 2;
//            NSString *gString = [cString substringWithRange:range];
//            range.location = 4;
//            NSString *bString = [cString substringWithRange:range];
//            // Scan values
//            unsigned int r, g, b;
//            [[NSScanner scannerWithString:rString] scanHexInt:&r];
//            [[NSScanner scannerWithString:gString] scanHexInt:&g];
//            [[NSScanner scannerWithString:bString] scanHexInt:&b];
//            
//            self.backgroundColor = [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0];
//        }
//        
//        return self;
//    };
//}
//
//-(DHViewFloatBlock)dc_cornerRadius{
//    return ^(CGFloat f){
//        self.layer.cornerRadius = f;
//        self.layer.masksToBounds = f > 0;
//        return self;
//    };
//}
//-(DHViewFloatBlock)dc_borderWidth{
//    return ^(CGFloat f){
//        self.layer.borderWidth = f;
//        return self;
//    };
//}
//-(DHViewColorBlock)dc_borderColor{
//    return ^(UIColor * color){
//        self.layer.borderColor = color.CGColor;
//        return self;
//    };
//}
//-(DHViewColorHexBlock)dc_borderColorHex{
//    return ^(NSString * colorHex){
//        
//        NSString *cString = [[colorHex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//        
//        // String should be 6 or 8 characters
//        if ([cString length] > 6) {
//            // 判断前缀
//            if ([cString hasPrefix:@"0X"])
//                cString = [cString substringFromIndex:2];
//            if ([cString hasPrefix:@"#"])
//                cString = [cString substringFromIndex:1];
//            if ([cString length] != 6)
//                return self;
//            // 从六位数值中找到RGB对应的位数并转换
//            NSRange range;
//            range.location = 0;
//            range.length = 2;
//            //R、G、B
//            NSString *rString = [cString substringWithRange:range];
//            range.location = 2;
//            NSString *gString = [cString substringWithRange:range];
//            range.location = 4;
//            NSString *bString = [cString substringWithRange:range];
//            // Scan values
//            unsigned int r, g, b;
//            [[NSScanner scannerWithString:rString] scanHexInt:&r];
//            [[NSScanner scannerWithString:gString] scanHexInt:&g];
//            [[NSScanner scannerWithString:bString] scanHexInt:&b];
//            
//            self.layer.borderColor = [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0].CGColor;
//        }
//        
//        return self;
//    };
//}
//-(DHViewBOOLBlock)dc_clipsToBounds{
//    return ^(BOOL bol){
//        self.clipsToBounds = bol;
//        return self;
//    };
//}
//-(DHViewFloatBlock)dc_alpha{
//    return ^(CGFloat f){
//        self.alpha = f;
//        return self;
//    };
//}
//-(DHViewBOOLBlock)dc_opaque{
//    return ^(BOOL bol){
//        self.opaque = bol;
//        return self;
//    };
//}
//-(DHViewBOOLBlock)dc_hidden{
//    return ^(BOOL bol){
//        self.hidden = bol;
//        return self;
//    };
//}
//-(DHViewUIViewContentModeBlock)dc_contentMode{
//    return ^(UIViewContentMode model){
//        self.contentMode = model;
//        return self;
//    };
//}
//
//-(DHViewBOOLBlock)dc_userInteractionEnabled{
//    return ^(BOOL boolValue){
//        self.userInteractionEnabled = boolValue;
//        return self;
//    };
//}


@end



#pragma mark - 分类 关于view的边框 切角
@implementation UIView (BorderLine)

- (UIView *)dc_borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
    
    if (borderType == UIBorderSideTypeAll) {
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = color.CGColor;
        return self;
    }
    
    
    /// 左侧
    if (borderType & UIBorderSideTypeLeft) {
        /// 左侧线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// 右侧
    if (borderType & UIBorderSideTypeRight) {
        /// 右侧线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// top
    if (borderType & UIBorderSideTypeTop) {
        /// top线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
    }
    
    /// bottom
    if (borderType & UIBorderSideTypeBottom) {
        /// bottom线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    return self;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    shapeLayer.path = bezierPath.CGPath;
    /// 线宽度
    shapeLayer.lineWidth = borderWidth;
    return shapeLayer;
}


#pragma mark - 任意切角
- (void)dc_cornerRadius:(CGFloat)readius rectCorner:(UIRectCorner)rectCorner
{
    CGRect bounds = self.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(readius, readius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;
    [self.layer addSublayer:maskLayer];
    self.layer.mask = maskLayer;
}

#pragma mark - 切角
- (void)dc_cornerRadius:(CGFloat)readius
{
    self.clipsToBounds = YES;
    self.layer.cornerRadius = readius;
}


#pragma mark - 边框  切角
- (void)dc_layerBorderWith:(CGFloat)width color:(UIColor *)color radius:(CGFloat)radius
{
    if (width && width > 0) {
        self.layer.borderWidth = width;
        self.layer.borderColor = color.CGColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = radius;
    }else{
        self.clipsToBounds = YES;
        self.layer.cornerRadius = radius;
    }
}





// 设置 cell 上左上右为圆角
- (void)setTopRoundedCorners {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                         cornerRadii:CGSizeMake(18.0, 18.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

// 设置 cell 下左下右为圆角
- (void)setBottomRoundedCorners {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                         cornerRadii:CGSizeMake(18.0, 18.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

// 取消圆角
- (void)resetRoundedCorners {
    self.layer.mask = nil;
}

// 设置 cell 的四个角为圆角
- (void)setRoundedCorners {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:UIRectCornerAllCorners
                                                         cornerRadii:CGSizeMake(18.0, 18.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}



@end
